/*
 * Created on May 3, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ar.edu.utn.frba.gdaia.src.externalinterface;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @author Patricio Implementacion de la interface de texto. Funciona de manera
 *         bloqueante. Se lo puede imaginar como dos atributos sincronizados y
 *         bloqueantes a la vez.
 */
public class ConcreteGUIInterface implements TextInterface {

	private Notificable notificable;

	private BlockingQueue uiToMind;

	private BlockingQueue mindToUI;

	public ConcreteGUIInterface() {
		uiToMind = new SynchronousQueue();
		mindToUI = new SynchronousQueue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#putFromUIToMind(ar.edu.utn.frba.gdaia.externalinterface.Mensaje)
	 */
	public void putFromUIToMind(Mensaje m) {
		try {
			uiToMind.put(m);
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#getFromUIToMind()
	 */
	public Mensaje getFromUIToMind() {
		try {
			return (Mensaje) uiToMind.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#putFromMindToUI(ar.edu.utn.frba.gdaia.externalinterface.Mensaje)
	 */
	public void putFromMindToUI(Mensaje m) {
		try {
			this.notifyObserver();
			mindToUI.put(m);
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#getFromMindToUI()
	 */
	public Mensaje getFromMindToUI() {
		try {
			return (Mensaje) mindToUI.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public Notificable getNotificable() {
		return notificable;
	}

	public void setNotificable(Notificable notificable) {
		this.notificable = notificable;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#setObserver(ar.edu.utn.frba.gdaia.externalinterface.Notificable)
	 */
	public void setObserver(Notificable notificable) {
		this.setNotificable(notificable);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.edu.utn.frba.gdaia.externalinterface.TextInterface#notifyObserver()
	 */
	public void notifyObserver() {
		this.getNotificable().update();
	}
}
